package com.thfw.test;

import android.content.Intent;
import android.util.Log;

import androidx.multidex.MultiDexApplication;

import com.thfw.test.utils.ConfigurationService;

import java.io.File;


import scooper.cn.sc_base.log.LogConfig;
import scooper.cn.sc_base.log.SCLog;
import utils.AppActivities;
import utils.exception.CrashHandler;

public class MyApplication extends MultiDexApplication {
    private static final String TAG = MyApplication.class.getCanonicalName();
    private ConfigurationService mSipConfigurationService;

    @Override
    public void onCreate() {
        super.onCreate();

        initLogPrint();
        initConfig();
    }

    private void initConfig() {
        mSipConfigurationService = new ConfigurationService(this);
    }

    public ConfigurationService getConfigurationService() {
        return mSipConfigurationService;
    }


    //日志模块初始化
    private void initLogPrint() {
        try {
            String directory = "thfw" + File.separator + this.getPackageName() +  File.separator +  "log" +File.separator +  "biz_log";
            LogConfig logConfig = new LogConfig.Builder()
                    .setDirectory(directory)
                    .setDaily(true)
                    .setRetentionPeriodDay(7)
                    .setFileNameSuffix("txt")
                    .build();
            SCLog.setDefaultLogConfig(logConfig);
            //sip流程日志打印在单独目录
            String sipPackageName;
            Package sipPackage =  SCLog.class.getPackage();
            if (sipPackage != null) {
                sipPackageName = sipPackage.getName();
            } else {
                sipPackageName = "cn.thfw.sip_log";
            }
            String sipDirectory = "thfw" + File.separator + this.getPackageName() +  File.separator +  "log"+  File.separator + "sip_log";
            LogConfig sipConfig = new LogConfig.Builder()
                    .setPackageName(sipPackageName)
                    .setDirectory(sipDirectory)
                    .setDaily(true)
                    .setRetentionPeriodDay(7)
                    .setFileNamePrefix("sip_log")
                    .setFileNameSuffix("log")
                    .build();

            SCLog.initLogConfig(sipConfig);
        } catch (Exception e) {
            //可能是权限问题
        }
        CrashHandler.getInstance().init(getApplicationContext(), this.getPackageName());
    }

    /**
     * exit application
     */
    public void exit() {
        Log.i(TAG, "exit()");
        onExit();
        AppActivities.instance().finishAll();
        //
        Log.i(TAG, "System::exit(0)");
        System.exit(0);

    }

    void onExit(){
        //退出操作
    };

}
